package top.huhuiyu.teachproject.base;

import lombok.Data;

import java.io.Serializable;

/**
 * 基础应答结果对象
 *
 * @author 胡辉煜
 */
@Data
public class BaseResult implements Serializable {
  private static final long serialVersionUID = 1L;
  public static final int ERROR_CODE = 500;
  public static final int SUCCESS_CODE = 200;

  private int code = ERROR_CODE;
  private String message;
  private boolean success;
  private String token;

  public static BaseResult instance(int code, String message, boolean success) {
    BaseResult result = new BaseResult();
    result.buildInfo(code, message, success);
    return result;
  }

  public void buildInfo(int code, String message, boolean success) {
    this.setSuccess(success);
    this.setCode(code);
    this.setMessage(message);
  }

  public static BaseResult fail(int code, String message) {
    return instance(code, message, false);
  }

  public static BaseResult fail(String message) {
    return instance(ERROR_CODE, message, false);
  }

  public static BaseResult ok(int code, String message) {
    return instance(code, message, true);
  }

  public static BaseResult ok(String message) {
    return ok(SUCCESS_CODE, message);
  }

}
